package com.sec.android.app.myfiles.feature.cloud;

import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import com.samsung.android.sdk.slinkcloud.CloudGatewayConstants;
import com.samsung.android.sdk.slinkcloud.CloudGatewayDeviceInfoUtils;
import com.samsung.android.sdk.slinkcloud.CloudGatewayMediaStore;
import com.samsung.android.sdk.slinkcloud.CloudGatewayStorageUtils;
import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.CloudType;
import com.sec.android.app.myfiles.module.cloud.CloudFileRecord;
import com.sec.android.app.myfiles.util.UiUtils;

/**
 * Created by jaebae on 2016-01-28.
 */
public abstract class AbsCloudImp {
    Context mContext;

    private boolean mAccountRetrieved;
    private int mDeviceId;
    private boolean mSignedIn;
    private String mAccountId;
    private long mTotalSize;
    private long mUsedSize;

    private CloudFileRecord mHomeRecord;


    AbsCloudImp(Context context) {
        mContext = context;
    }

    public boolean retrieveAccountInfo() {
        mAccountRetrieved = false;
        try (Cursor cursor = getAccountInfoCursor()) {
            if (cursor != null && (cursor.getCount() > 0) && (cursor.moveToFirst())) {
                mDeviceId = cursor.getInt(cursor.getColumnIndexOrThrow(CloudGatewayMediaStore.DeviceColumns._ID));
                mAccountId = cursor.getString(cursor.getColumnIndexOrThrow("web_storage_user_id"));
                mSignedIn = cursor.getInt(cursor.getColumnIndexOrThrow(CloudGatewayMediaStore.DeviceColumns.WEB_STORAGE_IS_SIGNED_IN)) != 0;
                mSignedIn |= ((mAccountId != null) && (!mAccountId.isEmpty()));
                mTotalSize = cursor.getLong(cursor.getColumnIndexOrThrow(CloudGatewayMediaStore.DeviceColumns.WEB_STORAGE_TOTAL_CAPACITY));
                mUsedSize = cursor.getLong(cursor.getColumnIndexOrThrow(CloudGatewayMediaStore.DeviceColumns.WEB_STORAGE_USED_CAPACITY));
                Log.d(this, "retrieveAccountInfo : signed in " + mSignedIn);
                Log.d(this, "retrieveAccountInfo : deviceId  " + mDeviceId);
                Log.d(this, "retrieveAccountInfo : accountId " + mAccountId);
                Log.d(this, "retrieveAccountInfo : total size " + mTotalSize);
                Log.d(this, "retrieveAccountInfo : used size " + mUsedSize);
                mHomeRecord = null;
                mAccountRetrieved = true;

                _retrieveAccountInfo();
            }
        }

        return mAccountRetrieved;
    }

    protected abstract void _retrieveAccountInfo();

    public Cursor getAccountInfoCursor() {
        ContentResolver resolver = mContext.getContentResolver();
        String selection = "web_storage_type=?";
        String[] selectionArgs = new String[]{
                getCloudName()
        };

        return resolver.query(CloudGatewayMediaStore.Device.CONTENT_URI, null, selection, selectionArgs, null);
    }

    public boolean isSignedIn() {
        return mSignedIn;
    }

    public int getDeviceId() {
        return mDeviceId;
    }

    public String getUserId() {
        return mAccountId;
    }

    public boolean isAccountRetrieved() {
        return mAccountRetrieved;
    }

    public void setTotalSize(long totalSize) {
        mTotalSize = totalSize;
    }

    public long getTotalSize() {
        return mTotalSize;
    }

    public void setUsedSize(long usedSize) {
        mUsedSize = usedSize;
    }

    public long getUsedSize() {
        return mUsedSize;
    }

    public boolean login(Fragment resultFragment) {
        boolean bRet = false;

        Bundle resultBundle = CloudGatewayDeviceInfoUtils.getInstance(mContext).getVersatileInformation("READY_TO_LOGIN", mDeviceId);

        if ((resultBundle != null) && (resultBundle.getBoolean("result") == true)) {
            Intent intent = CloudGatewayStorageUtils.getInstance(mContext).createStorageSignInIntent(getCloudName());
            intent.putExtra(CloudGatewayConstants.SLINK_UI_APP_THEME, true);
            resultFragment.startActivityForResult(intent, getRequestCode());
            bRet = true;
        } else {
            UiUtils.showToast(mContext, R.string.db_not_ready);
        }

        return bRet;
    }

    public FileRecord getCloudHomeRecord() {
        if (mHomeRecord == null) {
            mHomeRecord = (CloudFileRecord) FileRecord.createFileRecord(FileRecord.StorageType.Cloud, getStoragePath());
            mHomeRecord.setFileId(CloudFileRecord.CLOUD_ROOT);
            mHomeRecord.setDeviceId(getDeviceId());
        }

        return mHomeRecord;
    }

    public abstract CloudType getCloudType();

    public abstract String getCloudName();

    public abstract String getStoragePath();

    public abstract String getAccountType();

    public abstract int getRequestCode();
}
